package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.saas.domain.dao.CustomerStageDao;
import com.xbongbong.saas.domain.entity.CustomerStageEntity;
import com.xbongbong.saas.model.CustomerStageModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author haibin.zhang
 * @version v1.0
 * @date 2019/2/13 11:05
 * @since v1.0
 */
@Service
public class CustomerStageModelImpl implements CustomerStageModel {

    @Resource
    private CustomerStageDao customerStageDao;

    @Override
    public Integer insert(CustomerStageEntity entity) throws XbbException {
        entity.setAddTime((long) DateUtil.getInt());
        entity.setUpdateTime((long) DateUtil.getInt());
        customerStageDao.insert(entity);
        return entity.getId().intValue();
    }

    @Override
    public Integer update(CustomerStageEntity entity) throws XbbException {
        entity.setUpdateTime((long) DateUtil.getInt());
        customerStageDao.update(entity);
        return entity.getId().intValue();
    }

    @Override
    public List<CustomerStageEntity> findEntitys(Map<String, Object> param) {
        return customerStageDao.findEntitys(param);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return customerStageDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public CustomerStageEntity getByKey(Long key, String corpid) {
        return customerStageDao.getByKey(key,corpid);
    }

    @Override
    public CustomerStageEntity getByCode(String corpid, Long formId, String code) {
        return customerStageDao.getByCode(corpid, formId, code);
    }

    @Override
    public Long save(CustomerStageEntity customerStageEntity) throws XbbException {
        if (customerStageEntity.getId() != null){
            update(customerStageEntity);
        }else {
            insert(customerStageEntity);
        }
        return customerStageEntity.getId();
    }

    @Override
    public List<CustomerStageEntity> getDefaultList() {
        Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("corpid","0");
        map.put("del",0);
        return findEntitys(map);
    }

    @Override
    public void insertBatch(List<CustomerStageEntity> list) {
        customerStageDao.insertBatch(list);
    }

    @Override
    public List<CustomerStageEntity> getList(String corpid,Long formId, Integer enable) {
        Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("corpid",corpid);
        map.put("formId",formId);
        if (Objects.nonNull(enable)) {
            map.put("enable", enable);
        }
        map.put("del",0);
        map.put("orderByStr","sort desc");
        return findEntitys(map);
    }

    @Override
    public void updateBatch(List<CustomerStageEntity> customerStageEntityList, String corpid) {
        customerStageDao.updateBatch(customerStageEntityList, corpid);
    }

    @Override
    public void updateBatchDefaultAndLinkOption(List<CustomerStageEntity> customerStageEntityList, String corpid) {
        customerStageDao.updateBatchDefaultAndLinkOption(customerStageEntityList, corpid);
    }

    @Override
    public void updateBatch4MirrorRollback(List<CustomerStageEntity> customerStageEntityList, String corpid) {
        customerStageDao.updateBatch4MirrorRollback(customerStageEntityList, corpid);
    }

    @Override
    public void updateBatch4Script(List<CustomerStageEntity> customerStageEntityList) {
        if(CollectionsUtil.isNotEmpty(customerStageEntityList)) {
            int size = customerStageEntityList.size();
            int page = (size - 1) / 1000;
            for (int currPage = 0; currPage <= page; currPage++) {
                int offset = currPage * 1000;
                customerStageDao.updateBatch4Script(customerStageEntityList.subList(offset, (size - offset < 1000) ? size : offset + 1000));
            }
        }
    }

    @Override
    public Integer insert4Script(CustomerStageEntity entity) {
        return customerStageDao.insert4Script(entity);
    }

    @Override
    public void insertBatch4Script(List<CustomerStageEntity> list) {
        customerStageDao.insertBatch4Script(list);
    }

    @Override
    public Integer deleteBatch(List<CustomerStageEntity> customerStageEntityList, String corpid) {
        return customerStageDao.deleteBatch(customerStageEntityList, corpid);
    }
}
